<?php
$this->Html->addCrumb('Ads main', '/admin/ads/main');
$this->Html->addCrumb('Coupons', '/admin/coupons');
$this->Html->addCrumb('Edit');
?>
<?php
$this->Html->scriptStart(array('inline' => false));
echo '
      $(function(){
        $("#CouponAdminEditForm").TWS_Form( {
          validate : {
          rules : {
            CouponCompanyId : {required : true, help : "This is the company that the coupon is associated with."},
            CouponTitle : {required : true, help : "This is the title of the coupon.<br/><br/> e.g. 10% discount on all wedding dresses!"},
            CouponDescription : {required : true, help : "e.g. This is the description of the coupon.<br/><br/>Get 10% off of any Vera Wang wedding dress or accessories. Shoes are included too!"},
            CouponUrl : {url : true, help :"This can be used if the vendor has a coupon on their website and you wish to provide a direct link to their coupon. If this is populated, the link will go to their coupon page."},
            CouponStartDate :{date: true, help :"This is the start date that the coupon will be shown on the IsleVow website", required : true},
            CouponEndDate : {date:true, help: "This is the end date that the coupon will be shown until on the IsleVow website", required : true},
            CouponValidStartDate : {date:true, help:"This is the valid from start date that will be displayed on the coupon", required : true},
            CouponValidEndDate : {date:true, help:"This is the expiration date that will be displayed on the coupon", required : true},
            CouponImagePath : {help:"This is an optional image that could be uploaded that will be displayed on the coupon"}
          }
          }});
     
         $("#deleteBtn").click(function(event){
          if(!$("#deleteBtn").attr("data-delete-confirm")) {
          $("#deleteBtn").attr("data-delete-confirm","");
            $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this coupon? <br/><br/>", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); $("#deleteBtn").attr("data-delete-confirm",true).trigger("click");}}}});
            event.preventDefault();
          }
        });
         $("#deleteImage").click(function(event){
            var $link = $(this);
            $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this image?", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); location.href=$link.attr("href");}}}});
            event.preventDefault();          
        });
      });
       ';
$this->Html->scriptEnd();

?>


<h1>Edit Coupon</h1>
<?php echo $this->Session->flash();?>
<?php echo '<div id="actionLink">'.$this->Html->link('Back to list of Coupons',array('action'=>'index', 'admin'=>true)).'</div>'; ?>

<?php echo $this->element('errors', array('errors' => isset($errors)? $errors : null)); ?>
<?php

echo $this->TWSForm->create('Coupon',  array(
    'inputDefaults' => array(
        'div' => array('class'=>'row')),
        'class'=>'inlineForm',
        'type'=>'file'));
echo $this->TWSForm->input('company_id', array('name'=>'CouponCompanyId', 'type'=>'select', 'label'=>'<span class="required">*</span>Company', 'options'=>$options));
echo $this->TWSForm->input('title', array('name'=>'CouponTitle', 'size'=>'63', 'type'=>'text', 'label'=>'<span class="required">*</span>Title'));
echo $this->TWSForm->input('description', array('rows'=>'5','cols'=>'60','name'=>'CouponDescription', 'label'=>'<span class="required">*</span>Description'));

echo $this->TWSForm->input('start_date', array('type'=>'text', 'rel'=>'date','name'=>'CouponStartDate', 'label'=>'<span class="required">*</span>Start Date'));
echo $this->TWSForm->input('end_date', array('type'=>'text', 'rel'=>'date','name'=>'CouponEndDate', 'label'=>'<span class="required">*</span>End Date'));

echo $this->TWSForm->input('valid_start_date', array('type'=>'text', 'rel'=>'date','name'=>'CouponValidStartDate', 'label'=>'<span class="required">*</span>Coupon Valid From Date'));
echo $this->TWSForm->input('valid_end_date', array('type'=>'text', 'rel'=>'date','name'=>'CouponValidEndDate', 'label'=>'<span class="required">*</span>Coupon Valid To Date'));

echo $this->TWSForm->input('url', array('name'=>'CouponUrl', 'type'=>'text'));

$record = $this->request->data['Coupon'];
if(!empty($record['image_path'])){
  echo '<p>'.$this->Html->image($record['image_path'], array('alt' => $record['title'])).'</p>';
  echo '<p>'.$this->Html->link('Delete image', array('controller' => 'coupons', 'action' => 'deleteImage', 'admin'=>true, $record['id']), array("title"=>"Delete this image" , "id"=>"deleteImage")).'</p>';
  echo $this->TWSForm->input('_image_path', array('type'=>'hidden', 'name'=>'_image_path', 'value'=>$record['image_path']));
  } else {
  echo $this->TWSForm->input('image_path', array('type'=>'file', 'label'=>'Upload image', 'name'=>'CouponImagePath'));
}

echo $this->TWSForm->submit('Update', array('id'=>'editBtn', 'name'=>'editBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->submit("Delete", array('id'=>'deleteBtn','name'=>'deleteBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->end();

?>